class Solution:
  def __init__(self):
    self.res = 0
  
  def dfs(self, tree, node_index):
    # print('node_index={}'.format(node_index))
    if node_index * 2 < len(tree):
      self.res += tree[node_index] ^ tree[node_index * 2]
      # print('start={}, end={}'.format(node_index, node_index * 2))
      self.dfs(tree, node_index * 2)
    if node_index * 2 + 1 < len(tree):
      self.res += tree[node_index] ^ tree[node_index * 2 + 1]
      # print('start={}, end={}'.format(node_index, node_index * 2 + 1))
      self.dfs(tree, node_index * 2 + 1)

  def tree1(self, a: list):
    a.insert(0, 0)
    self.dfs(a, 1)
    return self.res


if __name__ == "__main__":
  solution = Solution()
  print(solution.tree1([1, 2, 3, 4, 5]))